<?php
class BaseController extends CController
{
	public function __construct($id,$module=null){
		parent::__construct($id,$module);
		// If there is a post-request, redirect the application to the provided url of the selected language
		if(isset($_POST['language'])) {
			$lang = $_POST['language'];
			$MultilangReturnUrl = $_POST[$lang];
			$this->redirect($MultilangReturnUrl);
		}
		// Set the application language if provided by GET, session or cookie
		if(isset($_GET['language'])) {
			Yii::app()->language = $_GET['language'];
			Yii::app()->user->setState('language', $_GET['language']);
			$cookie = new CHttpCookie('language', $_GET['language']);
			$cookie->expire = time() + (60*60*24*365); // (1 year)
			Yii::app()->request->cookies['language'] = $cookie;
		}
		else if (Yii::app()->user->hasState('language'))
			Yii::app()->language = Yii::app()->user->getState('language');
		else if(isset(Yii::app()->request->cookies['language']))
			Yii::app()->language = Yii::app()->request->cookies['language']->value;
	}
	public function createMultilanguageReturnUrl($lang='en'){
		if (count($_GET)>0){
			$arr = $_GET;
			$arr['language']= $lang;
		}
		else
			$arr = array('language'=>$lang);
		return $this->createUrl('', $arr);
	}
	// флеш-нотис пользователю
	public function setNotice($message)
	{
		return Yii::app()->user->setFlash('notice', $message);
	}

	// флеш-ошибка пользователю
	public function setError($message)
	{
		return Yii::app()->user->setFlash('error', $message);
	}
	//
	public function material_image($id, $image, $width='150', $path, $class='material_img')
	{
		if (!empty( $path ) ) 
		{
			$src = '/upload/' . $path . '/';
			$path = $_SERVER ['DOCUMENT_ROOT'] . '/upload/' . $path . '/';
		} else {
			$src = '/upload/';
			$path = $_SERVER ['DOCUMENT_ROOT'] . '/upload/';
		}
		if ($width<=70){
			$resize_w = 70;
			$resize_path = $path.'l/';
			$src .= 'l/';
		} elseif ($width<=250){
			$resize_w = 250;
			$resize_path = $path.'m/';
			$src .= 'm/';
		} else {
			$resize_path = $path;
		}
		if (isset ( $image ) && file_exists ( $path . $image ) && $image != NULL) {
			if (!file_exists ( $resize_path . $image )) {
				if(!file_exists($resize_path)){
					mkdir($resize_path, 0755, true);
				}
				$simage = new SimpleImage();
				$simage->load($path.$image);
				$simage->resizeToWidth($resize_w);
				$simage->save($resize_path . $image);
			}
			
			return CHtml::image ( $src . $image, $id, array (
				'width' => $width,
				'class' => $class
			));
		} else {
			return CHtml::image ( Yii::app ()->getBaseUrl ( true ) . '/upload/noimage.gif', 'Нет картинки', array (
				'width' => $width,
				'class' => $class 
			));
		}
	}
	public function material_image_src($id, $image, $width='150', $path, $class='material_img')
	{
		if (!empty( $path ) )
		{
			$src = '/upload/' . $path . '/';
			$path = $_SERVER ['DOCUMENT_ROOT'] . '/upload/' . $path . '/';
		} else {
			$src = '/upload/';
			$path = $_SERVER ['DOCUMENT_ROOT'] . '/upload/';
		}
		if ($width<=50){
			$resize_w = 50;
			$resize_path = $path.'l/';
			$src .= 'l/';
		} elseif ($width<=200){
			$resize_w = 200;
			$resize_path = $path.'m/';
			$src .= 'm/';
		} else {
			$resize_path = $path;
		}
		if (isset ( $image ) && file_exists ( $path . $image ) && $image != NULL) {
			if (!file_exists ( $resize_path . $image )) {
				if(!file_exists($resize_path)){
					mkdir($resize_path, 0755, true);
				}
				$simage = new SimpleImage();
				$simage->load($path.$image);
				$simage->resizeToWidth($resize_w);
				$simage->save($resize_path . $image);
			}
	
			return $src;
		} else {
			return Yii::app ()->getBaseUrl ( true ) . '/upload/noimage.gif';
		}
	}
	//save
	public function savePhoto($photoVal, $path){
		$info = pathinfo($photoVal['name']);
		$fileName = time() . '.' . $info['extension'];
		if(!is_dir($path)){
			mkdir($path);
		}
		$filepath = $path.$fileName;
		move_uploaded_file($photoVal['tmp_name'], $filepath);
		return $fileName;
	}
	//save
	public function saveMusic($musicVal, $model, $path){
		$file = CUploadedFile::getInstance($model,'file');
		if ($file)	{
			if($musicVal!=NULL && file_exists($path.$musicVal))
			{
				if($musicVal != 'noimage.gif')
					unlink($path.$musicVal);
			}
			$sourcePath = pathinfo($file->getName());
			$fileName = time() .'.'. $sourcePath['extension'];
			$model->file = $fileName;
			$model->save();
			//$path .= $model->getPrimaryKey().'/';
			if(!is_dir($path)){
				mkdir($path);
			}
			$filepath = $path.$fileName;
			$file->saveAs($filepath);
			return $fileName;
		}
	}
	//save
	public function saveImage($imgVal, $model, $path){
		$image = CUploadedFile::getInstance($model,'image');
		if ($image)	{
			if($imgVal!=NULL && file_exists($path.$imgVal))
			{
				if($imgVal != 'noimage.gif')
					unlink($path.$imgVal);
			}
			$sourcePath = pathinfo($image->getName());
			$fileName = time() .'.'. $sourcePath['extension'];
			$model->avatar = $fileName;
			$model->save();
			$path .= $model->getPrimaryKey().'/';
			if(!is_dir($path)){
				mkdir($path);
			}
			$file = $path.$fileName;
			$image->saveAs($file);
			return $fileName;
		}
	}
	// save URL
	public function saveImageUrl($imgVal, $model, $path){
		$image = CUploadedFile::getInstance($model,'image');
		if ($image)	{
			if($imgVal!=NULL && file_exists($path.$imgVal))
			{
				if($imgVal != 'noimage.gif')
					unlink($path.$imgVal);
			}
			$sourcePath = pathinfo($image->getName());
			$fileName = time() .'.'. $sourcePath['extension'];
			$model->avatar = $fileName;
			$model->save();
			//$path .= $model->getPrimaryKey().'/';
			if(!is_dir($path)){
				mkdir($path);
			}
			$file = $path.$fileName;
			$image->saveAs($file);
			return $fileName;
		}
	}
	//save Banner
	public function saveImageBanner($imgVal, $model, $path){
		$image = CUploadedFile::getInstance($model,'image');
		if ($image)	{
			if($imgVal!=NULL && file_exists($path.$imgVal))
			{
				if($imgVal != 'noimage.gif')
					unlink($path.$imgVal);
			}
			$sourcePath = pathinfo($image->getName());
			$fileName = time() .'.'. $sourcePath['extension'];
			$model->banner_img = $fileName;
			$model->save();
			// $path .= $model->getPrimaryKey().'/';
			if(!is_dir($path)){
				mkdir($path);
			}
			$file = $path.$fileName;
			$image->saveAs($file);
			return $fileName;
		}
	}
}
